import { logger } from '../core/logger';
import { cropInfo } from '../models/cropInfo';
import { clientBase, HOST } from './clientBase';
import { ajaxResult } from '../models/ajaxResult';
import axios, { AxiosError, AxiosResponse } from '@ohos/axios';

class cropClient extends clientBase {
  create(data: cropInfo): Promise<ajaxResult<string>> {
    return new Promise((resolve) => {
      axios.post<string, AxiosResponse<string>, any>(`${HOST}api/crops/create`, data, this.withToken).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: string = response.data.result;
          resolve(this.buildSuccessResult<string>(ret));
        } else
          resolve(this.buildFailedResult<string>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.instance.error(err);
        resolve(this.buildExceptionResult<string>(err));
      });
    });
  }

  update(data: cropInfo): Promise<ajaxResult<string>> {
    return new Promise((resolve) => {
      axios.post<string, AxiosResponse<string>, any>(`${HOST}api/crops/update`, data, this.withToken).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: string = response.data.result;
          resolve(this.buildSuccessResult<string>(ret));
        } else
          resolve(this.buildFailedResult<string>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.instance.error(err);
        resolve(this.buildExceptionResult<string>(err));
      });
    });
  }

  delete(cropId: string): Promise<ajaxResult<string>> {
    return new Promise((resolve) => {
      axios.delete<string, AxiosResponse<string>, any>(`${HOST}api/crops/del?cropId=${cropId}`, this.withToken).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: string = response.data.result;
          resolve(this.buildSuccessResult<string>(ret));
        } else
          resolve(this.buildFailedResult<string>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.instance.error(err);
        resolve(this.buildExceptionResult<string>(err));
      });
    });
  }

  getPages(pageIndex: number, keywords: string): Promise<ajaxResult<Array<cropInfo>>> {
    return new Promise((resolve) => {
      axios.get<string, AxiosResponse<string>, null>(`${HOST}api/crops/getPaging?pageIndex=${pageIndex}&keywords=${keywords}`, this.withToken).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: Array<cropInfo> = response.data.result?.data ?? [];
          resolve(this.buildSuccessResult<Array<cropInfo>>(ret));
        } else
          resolve(this.buildFailedResult<Array<cropInfo>>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.instance.error(err);
        resolve(this.buildExceptionResult<Array<cropInfo>>(err));
      });
    });
  }

  getAll(): Promise<ajaxResult<Array<cropInfo>>> {
    return new Promise((resolve) => {
      axios.get<string, AxiosResponse<string>, null>(`${HOST}api/crops/getAll`, this.withToken).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: Array<cropInfo> = response.data.result;
          resolve(this.buildSuccessResult<Array<cropInfo>>(ret));
        } else
          resolve(this.buildFailedResult<Array<cropInfo>>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.instance.error(err);
        resolve(this.buildExceptionResult<Array<cropInfo>>(err));
      });
    });
  }
}
export default new cropClient();